﻿using System;
using System.ComponentModel.Composition;
using System.Data;
using Kugar.Core.Data.SqliteHelper;
using Kugar.Core.ExtMethod;

namespace Kugar.Core.SMSMessage
{
    [Export(typeof(ISMSMessageLogProvider))]
    public class SMSMessageLogProvider_SQLite:ISMSMessageLogProvider
    {
        private SqliteHelper _helper = null;

        private const string _insertSendLogSQL = @"insert into tbl_SendMessageLog 
                                                       (ProviderName,Mobile,Message,Status,IsSuccess,ErrorMsg) 
                                                       values(@ProviderName,@Mobile,@Message,@Status,@IsSuccess,@ErrorMsg)";

        private const string _insertRevLogSQL = @"insert into tbl_ReceiveMessageLog 
                                                       (ProviderName,Mobile,Message,ReceiveDt) 
                                                       values(@ProviderName,@Mobile,@Message,@ReceiveDt)";

        public SMSMessageLogProvider_SQLite()
        {
            var connStr = Configuration.CustomConfigManager.Default.ConnectionSettings.GetValueByName<string>("SMSLog_SQLite");

            if (string.IsNullOrWhiteSpace(connStr))
            {
                throw new ArgumentNullException("数据库连接未配置,请设置\"SMSLog_SQLite\"的数据库连接", new Exception("数据库连接未配置,请设置\"SMSLog_SQLite\"的数据库连接"));
            }

            _helper=new SqliteHelper(connStr);
        }


        

        #region Implementation of IDisposable

        public void Dispose()
        {
            _helper.DisposeObject();
        }

        #endregion

        #region Implementation of ISMSMessageLogProvider

        public string Name { get { return "SQLite"; } }

        public void SendLog(string providerName, string mobile, string message, int status, bool isSuccess, string errorMsg)
        {
            var cmd = getSendLogCommand();

            cmd.Parameters.SetValue("@ProviderName", providerName);
            cmd.Parameters.SetValue("@Mobile", mobile);
            cmd.Parameters.SetValue("@Message", message);
            cmd.Parameters.SetValue("@Status", status);
            cmd.Parameters.SetValue("@IsSuccess", isSuccess);
            cmd.Parameters.SetValue("@ErrorMsg", errorMsg);

            try
            {
                _helper.ExecuteNoQuery(cmd);
            }
            catch (Exception)
            {
            }
        }

        public void ReceiveLog(string providerName, string mobile, string message, DateTime receiveDt)
        {
            var cmd = getRevLogCommand();

            cmd.Parameters.SetValue("@ProviderName", providerName);
            cmd.Parameters.SetValue("@Mobile", mobile);
            cmd.Parameters.SetValue("@Message", message);
            cmd.Parameters.SetValue("@ReceiveDt",receiveDt);    

            try
            {
                _helper.ExecuteNoQuery(cmd);
            }
            catch (Exception)
            {
            }
        }

        #endregion



        private System.Data.SQLite.SQLiteCommand getSendLogCommand()
        {
            var cmd = new System.Data.SQLite.SQLiteCommand(_insertSendLogSQL);

            cmd.AddParameters(new ADOCmdParam[]
                {
                    new ADOCmdParam("@ProviderName",DbType.String,""), 
                    new ADOCmdParam("@Mobile",DbType.String,""), 
                    new ADOCmdParam("@Message",DbType.String,""), 
                    new ADOCmdParam("@Status",DbType.Int32,0), 
                    new ADOCmdParam("@IsSuccess",DbType.Boolean,true), 
                    new ADOCmdParam("@ErrorMsg",DbType.String,"")
                });

            //cmd.CommandType = CommandType.Text;

            return cmd;
        }

        private System.Data.SQLite.SQLiteCommand getRevLogCommand()
        {
            var cmd = new System.Data.SQLite.SQLiteCommand(_insertRevLogSQL);

            cmd.AddParameters(new ADOCmdParam[]
                {
                    new ADOCmdParam("@ProviderName",DbType.String,""), 
                    new ADOCmdParam("@Mobile",DbType.String,""), 
                    new ADOCmdParam("@Message",DbType.String,""), 
                    new ADOCmdParam("@ReceiveDt",DbType.DateTime,DateTime.Now)
                });

            //cmd.CommandType = CommandType.Text;

            return cmd;
        }
    }
}